package com.verbetia.mbrainz.search;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.FilterIndexReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

public class TrackSearch {

	//static final File INDEX_DIR = new File("track_artist.index");
	static final File INDEX_DIR = new File("track_full.index");
	 
	static final float SCORE_MINIMAL = 0.9f;
	
	
	static TrackSearch instance;
	
	private IndexReader reader;

	
	public static void main(String[] args) {
		try {	
			TrackSearch.getInstance().searchTrack("intoxicados una vela");
		} catch(Exception e)
		{
			System.out.println(e);						
		}
	}
	
	
	static public  TrackSearch getInstance()
	{
		if (TrackSearch.instance == null)
			instance = new TrackSearch();
		
		return instance;
	}
	
	public TrackSearch()
	{
		try {
		   reader = IndexReader.open(INDEX_DIR);
		} catch (IOException e)
		{
			System.out.println(TrackSearch.INDEX_DIR.getAbsolutePath());
			System.out.println("cant read index!");
		}
	}
	
	public ArrayList<ResultItem> searchTrack(String q) 
		throws IOException, ParseException 
	{
		 //  IndexReader reader = IndexReader.open(INDEX_DIR);
		   
		   String queries="";
		   //String field="track";

		   ArrayList<ResultItem> results = new ArrayList<ResultItem>();
		   
		    Searcher searcher = new IndexSearcher(reader);
		    Analyzer analyzer = new StandardAnalyzer();
		    

		    String fields[] = { "track", "artist", "album" };
		    MultiFieldQueryParser qparser = new MultiFieldQueryParser(fields, analyzer);
	        //QueryParser parser = new QueryParser(field, analyzer);
	        		     
		      Query query = qparser.parse(q);
		      
		     // System.out.println("Searching for: " + query.toString(field));
		      
		      Hits hits = searcher.search(query);
		      System.out.println(hits.length());
		      
		      int end = 10;
		      if (hits.length()<10) end = hits.length();
		      
		      for (int start = 0; start < end /*hits.length()*/; start ++) 
		      {		    	  
		    	 
		    	  Document d = hits.doc(start);
		    	  
		    	  if (hits.score(start)> TrackSearch.SCORE_MINIMAL)
		    		  results.add(ResultItem.CreateItem(d.get("track"), 
		    				  							d.get("artist"),
		    				  							d.get("album"),
		    				  							d.get("asin"),
		    				  							hits.score(start)));
		    	  
		    			  						 		
		    	  System.out.println(d.get("artist") + " | " + d.get("track") + " | " + d.get("album") + " | " + d.get("asin") +  " | " + hits.score(start) );
		    	  
		      }	
		      
		      return results;
	}
}
